var ParticleSystem = require('ParticleSystem')
var Common = require('wy-Common')
cc.Class({
    extends: cc.Component,

    properties: {
        boss : cc.Node,
        pathNode : cc.Node
    },
    setShader: function(sprite, shaderName) {
      var glProgram = this.shaderPrograms[shaderName];
      if (!glProgram) {
        glProgram = new cc.GLProgram();
        var vert = require(cc.js.formatStr("%s.vert", shaderName));
        var frag = require(cc.js.formatStr("%s.frag", shaderName));
        glProgram.initWithString(vert, frag);
        if (!cc.sys.isNative) {  
          glProgram.initWithVertexShaderByteArray(vert, frag);
          glProgram.addAttribute(cc.macro.ATTRIBUTE_NAME_POSITION, cc.macro.VERTEX_ATTRIB_POSITION);  
          glProgram.addAttribute(cc.macro.ATTRIBUTE_NAME_COLOR, cc.macro.VERTEX_ATTRIB_COLOR);  
          glProgram.addAttribute(cc.macro.ATTRIBUTE_NAME_TEX_COORD, cc.macro.VERTEX_ATTRIB_TEX_COORDS);  
        }
        glProgram.link();  
        glProgram.updateUniforms();
        this.shaderPrograms[shaderName] = glProgram;
      }
      sprite._sgNode.setShaderProgram(glProgram);
      return glProgram;
    },
    getDateString()
    {
        let d = new Date();
        let str = d.getHours().toString();
        let timeStr = "";
        timeStr += (str.length==1? "0"+str : str) + ":";
        str = d.getMinutes().toString();
        timeStr += (str.length==1? "0"+str : str) + ":";
        str = d.getSeconds().toString();
        timeStr += (str.length==1? "0"+str : str) + ":";
        str = d.getMilliseconds().toString();
        if( str.length==1 ) str = "00"+str;
        if( str.length==2 ) str = "0"+str;
        timeStr += str;

        timeStr = "[" + timeStr + "]";
        return timeStr;
    },
    stack(index)
    {
        var e = new Error();
        var lines = e.stack.split("\n");
        lines.shift();
        var result = [];
        lines.forEach(function (line) {
            line = line.substring(7);
            var lineBreak = line.split(" ");
            if (lineBreak.length<2) {
                result.push(lineBreak[0]);
            } else {
                result.push({[lineBreak[0]] : lineBreak[1]});
            }
        });

        var list = [];
        if(index < result.length-1){
            for(var a in result[index]){
                list.push(a);
            }
        }

        var splitList = list[0].split(".");
        return (splitList[0] + ".js->" + splitList[1] + ": ");
    },
    log(...args)
    {
        // var backLog = console.log || cc.log// || log;
        // if(OPENLOGFLAG){
            // backLog.call(this,"%s%s:"+cc.js.formatStr.apply(cc,arguments),this.stack(2),this.getDateString());
        // }
        var logs = {};
        Object.assign(logs,args[0])
        Object.assign(logs,args[1])
        console.log(logs)
    },
    start(){
        function a(){
            Object.defineProperties(this,'b',{
                set:function(){
                    
                }
            })
        }

        var a = {
            get b(){
                return this._b;
            },
            set b(v){
                this._b = v;
            },
            _b : 30
        }
        console.log('a.b',a.b)
    },
    onClickStart(){
        this.pathNode.getComponent('PathReady').runAction(1);
        // this.path.runAction(1);
    },
    onTouch(event){
       this.tag.position = this.node.convertToNodeSpaceAR(event.getLocation())
    },

    getAngle(p1,p2){
      if(p1.x==p2.x){
          return p2.y > p1.y ? Math.PI/2 : -Math.PI
      }
      var a = Math.atan((p2.y-p1.y)/(p2.x-p1.x))
      // var a = Math.acos((p2.y-p1.y)/(p2.sub(p1).mag()))
      return a;
    },
    onParticleCreate(child){
      var speed = child.detail._particle.speedInit
      var p1 = this.particleParent.getPosition();
      var p2 = this.tag.getPosition();

      var len = p2.sub(p1).mag()
      var a = Math.acos((p2.x-p1.x)/len)
      var r = a;
      if(p2.y<p1.y){
        r = 2*Math.PI - r;
      }   
      var dx = Math.cos(r)*speed
      var dy = Math.sin(r)*speed
      child.detail._particle.speed = cc.v2(dx,dy)
      child.detail.node.rotation = 270-r*180/Math.PI
    }
});
